<?php
/**
 *	FILENAME: 		/server_app/new_registration.php
 *	DESCRIPTION:	This script creates a new MyNova Mobile user account
 *	AUTHOR:			Casey Burkhardt 
 *	VERSION:		1.1.0
 *	LAST MODIFIED:	12/3/10
 **/

require_once($_SERVER['DOCUMENT_ROOT'] . "/config/environment.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/functions.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/logging.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/users.php");

enforceSSL();
setRequestParameters();

$ldapUsername = $_PARAMETERS["username"];
$UDID = $_PARAMETERS["UDID"];
$pin = $_PARAMETERS["pin"];

if (strlen($ldapUsername) == 0 || strlen($UDID) == 0 || strlen($pin) == 0) {
    $response["response_type"] = "error";
    $response["error_desc"] = "Username, password, or UDID not provided.";
    logWarning("No user data supplied during authentication");
    echo(json_encode($response));
} else {
    $encryptedPin = encryptPin($pin);
    $uid = addNewRegisteredUser($ldapUsername, $encryptedPin, $UDID);
    if ($uid !== FALSE) {
        $response["response_type"] = "success";
        $response["uid"] = $uid;
        logAction("User with ID: " . $uid . " created successfully");
        echo(json_encode($response));
    } else {
        $response["response_type"] = "error";
        $response["error_desc"] = "User record was not successfully created";
        logError("Unable to create user account");
        echo(json_encode($response));
    }
}
?>
